#include "elementtableframe.h"
#include "ui_elementtableframe.h"
#include <QDebug>

ElementTableFrame::ElementTableFrame(QWidget *parent,AnalyteDataBase *pAnalyteDataBase,int type) :
    QFrame(parent),
    ui(new Ui::ElementTableFrame)
{
    ui->setupUi(this);
    this->type=type;
    this->pAnalyteDataBase=pAnalyteDataBase;

    pLabel=new QLabel(this);
    pListWidget=new QListWidget(this);
    ui->gridLayout->addWidget(pLabel,0,2,1,10);
    ui->gridLayout->addWidget(pListWidget,1,2,2,10);

    pButtonGroup=new QButtonGroup(this);
    connect(pButtonGroup,SIGNAL(buttonClicked(int)),this,SLOT(Onclicked(int)));
    QPushButton *pPushButton=ui->pushButton1;
    int i;
    for(i=1;i<=104;i++)//
    {
        pButtonGroup->addButton(pPushButton,i);
        pPushButton=qobject_cast<QPushButton*>(pPushButton->nextInFocusChain());
    }

    switch(type)
    {
    case 0:
        break;
    case 1://主页面
        pLabel->setVisible(false);
        pListWidget->setVisible(false);
        ui->horizontalWidget->setVisible(false);
        break;
    case 2://弹出框
        break;
    }

    connect(ui->pushButton_Cancel,SIGNAL(clicked()),this,SLOT(deleteLater()));
}

ElementTableFrame::~ElementTableFrame()
{
    delete ui;
}

void ElementTableFrame::Onclicked(int id)
{
    QString name=pButtonGroup->button(id)->text();
    emit SIG_Clicked(id,name);
    switch(type)
    {
    case 0:
        break;
    case 1://主页面
        break;
    case 2://弹出框
        QHash<QString,EleAttributes> elementTable=pAnalyteDataBase->GetElementTable();
        EleAttributes eleAttributes=elementTable.value(name);
        QHash<QString,EleAttributeBase> eleAttributeBases=eleAttributes.GetEleAttributes();

        pLabel->setText(QString("%1:RSF %2;原子序数%3;第一电离势能 %4;第二电离势能 %5;")
                        .arg(name).arg(eleAttributes.rsf).arg(eleAttributes.atomicNum)
                        .arg(eleAttributes.energyFirst).arg(eleAttributes.energySecond));
        int count=eleAttributeBases.count();
        qDebug()<<"count"<<count;
        pListWidget->clear();
        foreach(EleAttributeBase element,eleAttributeBases)
        {
            QString str=QString("%1%2 %3 %4").arg(qRound(element.massNum)).arg(name).arg(element.massNum).arg(element.abundance);
            pListWidget->addItem(str);
        }
        pListWidget->setCurrentRow(0);//
        break;
    }

}


void ElementTableFrame::on_pushButton_Ok_clicked()
{
    if(pListWidget->currentItem()==0) return;
    QString name=pListWidget->currentItem()->text();
    if(name!="")
    {
        QStringList str=name.split(" ");
        QString data0=str.at(0);
        QString data1=str.at(1);
        double massNum=data1.toDouble();
        int nucleus=qRound(massNum);
        emit SIG_ChooseEle(massNum,data0.remove(QString::number(nucleus)));

        qDebug()<<"name"<<name<<"str"<<str;
    }

    deleteLater();
}
